<script>
    import Vue from 'vue'
    Object.defineProperty(Vue.prototype, '$tableExpand', {
        get(){
            return {
                // 初始化数据源
                init(data, expandedAll){
                    return this._format(data, expandedAll)
                },
                _format(data, expandedAll, parent, level){
                    let tmp = []
                    Array.from(data).forEach((record) => {
                        if (record._expanded === undefined) {
                            Vue.set(record, '_expanded', expandedAll)
                        }
                        if (parent) {
                            Vue.set(record, '_parent', parent)
                        }
                        let _level = 0
                        if (level !== undefined && level !== null) {
                            _level = level + 1
                        }
                        Vue.set(record, '_level', _level)
                        tmp.push(record)
                        if (record.children && record.children.length > 0) {
                            let children = this._format(record.children,expandedAll, record, _level )
                            tmp = tmp.concat(children)
                        }
                    })
                    
                    return tmp
                },
                //设置el-table组件row-style,以控制行的显示隐藏
                rowStyle({ row, rowIndex }){
                    let show = (row._parent ? (row._parent._expanded && row._parent._show) : true)
                    row._show = show
                    return show ? '' : 'display:none;'
                },
            }
        }
    })
    export default {}
</script>